﻿#include "Config.h"
#include "tool.h"

Config::Config()
{
    memset(&confVar,0,sizeof(confVar));
}

Config::~Config()
{
}

QVariant Config::getConfig(const char *set, const char *key){
    QVariant ret = configIni->value(QString("/%0/%1").arg(set).arg(key));
    return ret;
}

QVariant Config::getConfVar(const char *key){
    return getConfig("CONF",key);
}

QVariant Config::getColorVar(const char *key){
    return getConfig("COLOR",key);
}

QVariant Config::getSaveVar(const char *key){
    return getConfig("SAVE",key);
}

QVariant Config::getEnlargeVar(const char *key){
    return getConfig("ENLARGE",key);
}

QVariant Config::getMenuVar(const char *key){
    return getConfig("MENU",key);
}

void Config::setConfName(const QString name){
    confName = name;
}

bool Config::readConfig(void)
{
    retInfo(confName);

    configIni = new QSettings(confName, QSettings::IniFormat);
    configIni->setIniCodec("UTF-8");

    // CONF
    qStrToCstr(getConfVar("Title").toString(),
               confVar.Title,sizeof(confVar.Title));
    retInfo(getConfVar("Title").toString());
    confVar.Start = qStrToInt(getConfVar("Start").toString());
    confVar.MouseWheelStep = qStrToInt(getConfVar("MouseWheelStep").toString());
    confVar.MouseMoveStep = qStrToInt(getConfVar("MouseMoveStep").toString());
    confVar.CameraIndex = qStrToInt(getConfVar("CameraIndex").toString());
    confVar.CameraTime = qStrToInt(getConfVar("CameraTime").toString());
    confVar.WindowStatus = qStrToInt(getConfVar("WindowStatus").toString());
    confVar.ImageGray = qStrToInt(getConfVar("ImageGray").toString());
    confVar.ImageResolution = qStrToInt(getConfVar("ImageResolution").toString());
    confVar.ImageInverse = qStrToInt(getConfVar("ImageInverse").toString());
    confVar.ColorSetup = qStrToInt(getConfVar("ColorSetup").toString());
    confVar.ImageVertical = qStrToInt(getConfVar("ImageVertical").toString());
    confVar.ImageHorizontal = qStrToInt(getConfVar("ImageHorizontal").toString());
    confVar.ImageRotate = qStrToInt(getConfVar("ImageRotate").toString());
    confVar.ImageZoom= qStrToInt(getConfVar("ImageZoom").toString());
    confVar.ImageZoomMax= qStrToInt(getConfVar("ImageZoomMax").toString());
    // COLOR
    colorVar.WinX = qStrToInt(getColorVar("WinX").toString());
    colorVar.WinY = qStrToInt(getColorVar("WinY").toString());
    colorVar.WinWidth = qStrToInt(getColorVar("WinWidth").toString());
    colorVar.WinHeight = qStrToInt(getColorVar("WinHeight").toString());
    colorVar.ContrastValue = qStrToInt(getColorVar("ContrastValue").toString());
    colorVar.ContrastMin = qStrToInt(getColorVar("ContrastMin").toString());
    colorVar.ContrastMax = qStrToInt(getColorVar("ContrastMax").toString());
    colorVar.BrightnessValue = qStrToInt(getColorVar("BrightnessValue").toString());
    colorVar.BrightnessMin = qStrToInt(getColorVar("BrightnessMin").toString());
    colorVar.BrightnessMax = qStrToInt(getColorVar("BrightnessMax").toString());
    colorVar.SaturationValue = qStrToInt(getColorVar("SaturationValue").toString());
    colorVar.SaturationMin = qStrToInt(getColorVar("SaturationMin").toString());
    colorVar.SaturationMax = qStrToInt(getColorVar("SaturationMax").toString());
    colorVar.GammaValue = qStrToInt(getColorVar("GammaValue").toString());
    colorVar.GammaMin = qStrToInt(getColorVar("GammaMin").toString());
    colorVar.GammaMax = qStrToInt(getColorVar("GammaMax").toString());
    //SAVE
    qStrToCstr(getSaveVar("SavePath").toString(),
               saveVar.SavePath,sizeof(saveVar.SavePath));
    qStrToCstr(getSaveVar("CurDir").toString(),
               saveVar.CurDir,sizeof(saveVar.CurDir));
    // ENLARGE
    enlargeVar.WinX = qStrToInt(getEnlargeVar("WinX").toString());
    enlargeVar.WinY = qStrToInt(getEnlargeVar("WinY").toString());
    enlargeVar.WinWidth = qStrToInt(getEnlargeVar("WinWidth").toString());
    enlargeVar.WinHeight = qStrToInt(getEnlargeVar("WinHeight").toString());
    enlargeVar.WinZoom = qStrToFloat(getEnlargeVar("WinZoom").toString());

    //MENU
    QStringList vList = getMenuVar("ActionList").toString().split(",");
    for(int i=0; i<vList.size(); i++){
        actionList.append(vList[i]);
    }
    vList = getMenuVar("ActionType").toString().split(",");
    for(int i=0; i<vList.size(); i++){
        actionType.append(vList[i].toInt());
    }
    vList = getMenuVar("ActionMark").toString().split(",");
    for(int i=0; i<vList.size(); i++){
        actionMark.append(vList[i].toInt());
    }
    retInfo(QString("%0 %1 %2").arg(actionList.count()).arg(actionType.count()).arg(actionMark.count()));
    bool ret = true;
    if(actionList.count() != actionType.count() || actionList.count() != actionMark.count()){
        ret = false;
    }

    delete configIni;
    return ret;
}

void Config::saveCamera(void){
    configIni = new QSettings(confName, QSettings::IniFormat);
    configIni->setIniCodec("UTF-8");

    configIni->setValue("/CONF/CameraIndex",QString("%0").arg(confVar.CameraIndex));

    delete configIni;
}

void Config::saveConfig(void){
    configIni = new QSettings(confName, QSettings::IniFormat);
    configIni->setIniCodec("UTF-8");

    configIni->setValue("/CONF/ColorSetup",QString("%0").arg(confVar.ColorSetup));
    configIni->setValue("/CONF/ImageGray",QString("%0").arg(confVar.ImageGray));
    configIni->setValue("/CONF/ImageResolution",QString("%0").arg(confVar.ImageResolution));
    configIni->setValue("/CONF/ImageInverse",QString("%0").arg(confVar.ImageInverse));
    configIni->setValue("/CONF/ImageVertical",QString("%0").arg(confVar.ImageVertical));
    configIni->setValue("/CONF/ImageHorizontal",QString("%0").arg(confVar.ImageHorizontal));
    configIni->setValue("/CONF/ImageRotate",QString("%0").arg(confVar.ImageRotate));
    configIni->setValue("/CONF/ImageZoom",QString("%0").arg(confVar.ImageZoom));

    delete configIni;
}

void Config::saveColor(void){
    configIni = new QSettings(confName, QSettings::IniFormat);
    configIni->setIniCodec("UTF-8");

    configIni->setValue("/COLOR/WinX",QString("%0").arg(colorVar.WinX));
    configIni->setValue("/COLOR/WinY",QString("%0").arg(colorVar.WinY));
    configIni->setValue("/COLOR/WinWidth",QString("%0").arg(colorVar.WinWidth));
    configIni->setValue("/COLOR/WinHeight",QString("%0").arg(colorVar.WinHeight));
    configIni->setValue("/COLOR/ContrastValue",QString("%0").arg(colorVar.ContrastValue));
    configIni->setValue("/COLOR/BrightnessValue",QString("%0").arg(colorVar.BrightnessValue));
    configIni->setValue("/COLOR/SaturationValue",QString("%0").arg(colorVar.SaturationValue));
    configIni->setValue("/COLOR/GammaValue",QString("%0").arg(colorVar.GammaValue));

    delete configIni;
}

void Config::saveSave(void){
    configIni = new QSettings(confName, QSettings::IniFormat);
    configIni->setIniCodec("UTF-8");

    configIni->setValue("/SAVE/CurDir",QString("%0").arg(saveVar.CurDir));

    delete configIni;
}

void Config::saveEnlarge(void){
    configIni = new QSettings(confName, QSettings::IniFormat);
    configIni->setIniCodec("UTF-8");

    retInfo(QString("saveEnlarge %0").arg(getNumber(enlargeVar.WinZoom)));
    configIni->setValue("/ENLARGE/WinX",QString("%0").arg(enlargeVar.WinX));
    configIni->setValue("/ENLARGE/WinY",QString("%0").arg(enlargeVar.WinY));
    configIni->setValue("/ENLARGE/WinWidth",QString("%0").arg(enlargeVar.WinWidth));
    configIni->setValue("/ENLARGE/WinHeight",QString("%0").arg(enlargeVar.WinHeight));
    configIni->setValue("/ENLARGE/WinZoom",QString("%0").arg(enlargeVar.WinZoom));

    delete configIni;

}
